package com.dbxt.common.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dbxt.common.service.CommonService;
import com.dbxt.dao.CommonMapper;
import com.dbxt.entity.Clerk;
import com.dbxt.entity.Student;
import com.dbxt.entity.Teacher;
import com.dbxt.util.MD5Util;
@Service
@Transactional
public class CommonServiceImpl implements CommonService{
	@Autowired
	private CommonMapper commonMapper;

	@Override
	public Student findOneStudent(String username) {
		return commonMapper.findOneStudent(username);
	}

	@Override
	public Teacher findOneTeacher(String username) {
		return commonMapper.findOneTeacher(username);
	}

	@Override
	public Clerk findOneClerk(String username) {
		return commonMapper.findOneClerk(username);
	}
	
	@Override
	public boolean stuentIsExist(String username) {
		System.out.println(commonMapper);
		Student findOneStudent = commonMapper.findOneStudent(username);
		if(findOneStudent!=null) {
			return true;
		}else {
			return false;
		}
	}
	@Override
	public boolean teacherIsExist(String username) {
		Teacher findOneTeacher = commonMapper.findOneTeacher(username);
		if(findOneTeacher!=null) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public boolean clerkIsExist(String username) {
		Clerk findOneClerk = commonMapper.findOneClerk(username);
		if(findOneClerk!=null) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	public Student studentLogin(String username, String password)  {
		boolean exist = this.stuentIsExist(username);
		if(exist) {
			try {
				String md5withkey = MD5Util.md5withkey(password, "dbxt");
				return  commonMapper.studentLogin(username, md5withkey);	
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
			
	}

	@Override
	public Teacher teacherLogin(String username, String password) {
		boolean exist = this.teacherIsExist(username);
		if(exist) {
			try {
				String md5withkey = MD5Util.md5withkey(password, "dbxt");
				return commonMapper.teacherLogin(username, md5withkey);		
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public Clerk clerkLogin(String username, String password) {
		boolean exist = this.clerkIsExist(username);
		if(exist) {
			try {
				String md5withkey = MD5Util.md5withkey(password, "dbxt");
				return commonMapper.clerkLogin(username, md5withkey);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public void changeStudentPassword(String password, Integer sid) {
		commonMapper.changeStudentPassword(password, sid);
	}

	@Override
	public void changeClerkPassword(String password, Integer cid) {
		commonMapper.changeClerkPassword(password, cid);
		
	}

	@Override
	public void changeTeacherPassword(String password, Integer tid) {
		commonMapper.changeTeacherPassword(password, tid);
		
	}

	

	

}
